import { request } from 'umi';
import getApiData, { getNestApiData } from "@/services/cpasRequest";

export async function createReport(params: { projectId: string; partId: string; dataId: string; ids: string[] }) {
  const data = await request('/cpasApi/report/create', {
    method: 'POST',
    data: params,
  });
  return data;
}

export async function openReport(params: {
  projectId: string;
  partId: string;
  version?: string;
  fileName: string;
}) {
  const data = await request('/cpasApi/report/openReport', {
    method: 'POST',
    data: params,
  });
  return data;
}


export async function initTemplate(params: {
  templateName: string;
  copyFilesToWhere: string;
}) {
  const data = await request('/cpasApi/template/onlyCopyFilesWhenInitTemplate', {
    method: 'POST',
    data: params,
  });
  return data;
}


export async function saveTemplate(params: {
  templateName: string;
  files: string;
  isInsertDB: boolean;
}) {
  const data = await request('/cpasApi/template/onlyCopyFilesWhenSaveTemplate', {
    method: 'POST',
    data: params,
  });
  return data;
}

// export async function saveTemplate(params: {
//   templateName: string;
//   files: string[];
//   type: string;
// }) {
//   const data = await request('/cpasApi/template/saveFilesToTemplate', {
//     method: 'POST',
//     data: params,
//   });
//   return data;
// }

export async function createVersion(params: {
  projectId: string;
  partId: string;
  version: string;
}) {
  const data = await request('/cpasApi/report/versions', {
    method: 'POST',
    data: params,
  });
  return data;
}

export async function getVersions(params: { projectId: string; partId: string }) {
  const data = await request(
    `/cpasApi/report/versions?projectId=${params.projectId}&partId=${params.partId}`,
    {
      method: 'GET',
    },
  );
  return data;
}

export async function queryAnyFileIfExistInDiskRelativePath(params: { filePath: string }) {
  const data = await request(
    `/cpasApi/template/queryAnyFileIfExistInDiskRelativePath?filePath=${params.filePath}`,
    {
      method: 'GET',
    },
  );
  return data;
}

export async function getDataId(params: { dbName: string }) {
  const data = await request(
    `/cpasApi/cpasTableEngine/getAuditYear?dbname=${params.dbName}`,
    {
      method: 'GET',
    },
  );
  return data;
}

export async function getNotes(params: { partId: string, dataId: string }) {
  const data = await request(
    `/cpasApi/disclosure/notes-simple?dbName=${params.partId}&dataId=${params.dataId}`,
    {
      method: 'GET',
    },
  );
  return data;
}

export async function exportReport(params: {
  projectId: string;
  partId: string;
  version: string;
  targetDir: string;
  isTemplate?:boolean
}) {
  const data = await request('/cpasApi/report/export', {
    method: 'POST',
    data: params,
  });
  return data;
}

export async function exportTempldate(params: {
  projectId: string;
  partId: string;
  targetDir: string;
}) {
  const data = await request('/cpasApi/report/importTemplate', {
    method: 'POST',
    data: params,
  });
  return data;
}


export async function getSeekAuditReportTemplate(params: {
  zcbfid: string;
}) {
  try {
    const res = await getApiData("account.seekAuditReportTemplate", params);
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        success: true,
      });
    }
    return Promise.resolve({
      data: '接口account.seekAuditReportTemplate出错了！',
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口account.seekAuditReportTemplate出错了！',
      success: false,
    });
  }
};


export async function gnoteTreeList(params: any) {
  try {
    const res = await getApiData("noteTree.getlist", params);
    return res;
  } catch (error) {
    return Promise.resolve({ data: error, ok: false, code: 100000 });
  }
}

export async function CellDataTidList(params: any) {
  try {
    const res = await getApiData("designCell.data.gettid", params);
    return res;
  } catch (error) {
    return Promise.resolve({ data: error, ok: false, code: 100000 });
  }
}



export async function exportExcelReport(params: { dbname: string, directoryPath: string,  projectName: string , zcbfid: string,resSetting:[]  }) {
  const aaa0:{reportDate:string}[] = params.resSetting.filter((a: any) => a.hide !== 1 && a.reportType === 0).sort((a: any, b: any) => b.id - a.id);
    const eeee:{reportDate:string}[] = params.resSetting.filter((a: any) => a.hide !== 1 && a.reportType === 1).sort((a: any, b: any) => b.id - a.id);
    let excelDate: string = " ";
    let excelYear: string = " ";
    if (eeee.length>2) {
      excelYear = " ";
      excelDate = " ";
    } else {
      if (aaa0[0]?.reportDate) {
        excelDate = aaa0[0].reportDate;
      } else {
        console.log(`1获取批量设置的日期报错！`, "error", 2000);
        excelDate = "{reportDate}";
      }
      if (eeee[0]?.reportDate) {
        excelYear = eeee[0].reportDate;
      } else {
        console.log(`2获取批量设置的日期报错！`, "error", 2000);
        excelYear = "{year}";
      }
    }
   


  let excelParams: any = {
    dbname: params.dbname,
    directoryPath: params.directoryPath,
    projectName: params.projectName,
    zcbfid: params.zcbfid,
    excelFileName: "财务报表", 
    isPageSet:true,
    excelInfo: [
      {
        cpastableParam: {
          tname: "审定财务-资产表",
          data_id: "",
          type: "fs",
        },
        // tableTitle: "资产负债表",
        tableDate: "2023-05-25",
        isExportTableTitle: true,
        exportType: "primary",
        signDirection: "vertical",
        // tableType:'_HBGS'
      },
      {
        cpastableParam: {
          tname: "审定财务-负债表",
          data_id: "",
          type: "fs",
        },
        // tableTitle: "资产负债表（续）",
        tableDate: "2023-05-25",
        isExportTableTitle: true,
        exportType: "primary",
        signDirection: "vertical",
        // tableType:'_HBGS'
      },
      {
        cpastableParam: {
          tname: "审定财务-利润表",
          data_id: "",
          type: "fs",
        },
        // tableTitle: "利润表",
        tableDate: "2023-05-25",
        isExportTableTitle: true,
        exportType: "primary",
        signDirection: "vertical",
        // tableType:'_HBGS'
      },
      {
        cpastableParam: {
          tname: "审定财务-现金流量表",
          data_id: "",
          type: "fs",
        },
        // tableTitle: "现金流量表",
        tableDate: "2023-05-25",
        isExportTableTitle: true,
        exportType: "primary",
        signDirection: "vertical",
        // tableType:'_HBGS'
      },
      {
        cpastableParam: {
          tname: "审定财务-所有者权益变动表_本年",
          data_id: "",
          type: "fs",
        },
        // tableTitle: "股东(或所有者)权益变动表",
        tableDate: "2023-05-25",
        isExportTableTitle: true,
        isColumnNum: true,
        exportType: "primary",
        signDirection: "horizontal",
        // tableType:'_GS'
      },
      {
        cpastableParam: {
          tname: "审定财务-所有者权益变动表_上年",
          data_id: "",
          type: "fs",
        },
        // tableTitle: "股东(或所有者)权益变动表",
        tableDate: "2023-05-25",
        isExportTableTitle: true,
        isColumnNum: true,
        exportType: "primary",
        signDirection: "horizontal",
        // tableType:'_GS'
      },
      {
        cpastableParam: {
          tname: "审定财务-资产减值准备情况表",
          data_id: "",
          type: "fs",
        },
        // tableTitle: "资产减值准备情况表",
        tableDate: excelDate,
        isExportTableTitle: true,
        isColumnNum: true,
        exportType: "primary",
        signDirection: "horizontal",
        // tableType:'_GS'
      },
    ],}
    excelParams.excelInfo.map(((itemParam: any, index:number) => {
      itemParam.tableDate = index < 2 || itemParam.cpastableParam.tname === "审定财务-资产减值准备情况表" ? excelDate :excelYear;
      return itemParam;
    }));

    

  const data = await request('/cpasApi/cpasTableEngine/getCpatableToBatchExportExcel', {
    method: 'POST',
    data: excelParams,
  }  );
 
  return data;
}


export async function notesIsEmpty(params: { zcbfid: string, dataId: string }) {
  try {
    const res = await getNestApiData( `/disclosure/notes-is-empty?dbname=${params.zcbfid}&dataId=${params.dataId}`,
     'GET'
    )
    if (res.ok) {
      return Promise.resolve({
        data: res.data,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.data,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口saveCpasDesc出错了！',
      success: false,
    });
  }
}